// pages/operation/operation.js
let app = getApp();
let request = require("../../utils/request");
import {
  getParkInfoListApi,
  getTicketbalanceApi,
  setAutoSettledApi,
} from "../../api/index.js";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    barHeight: wx.getMenuButtonBoundingClientRect().bottom,
    pageHeight: app.globalData.pageHeight,
    userInfo: {},
    callPhoneDialog: false,
    phoneNumber: "",
    parkingList: [],
    parkManagerList: [],
    currentPage: 1,
    pageNum: 1,
    pageSize: 30,
    canIUseGetUserProfile: false,
    auditActive: 1,
    managerId: "",
    parkId: "",
    ticketBlance: {},
    totalIncome: {},
    parkColumns: [],
    autoSettledStatus: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let data = wx.getStorageSync("parkInfo");
    this.setData({
      userInfo: {
        avatarUrl: data.avatar,
        nickName: data.nickname,
        phone: data.mobilePhone,
        isOwner: data.isOwner,
        realName: data.realName,
      },
    });
  },
  onShow() {
    // this.getParkManagerList();
    // this.getParkingList();
    // 查询已审核通过车场
    let selectPark = this.selectComponent("#selectPark"); // 页面获取自定义组件实例
    selectPark.getAuditedPark(); // 通过实例调用组件事件
    let parkId = wx.getStorageSync("parkId");
    let managerId = wx.getStorageSync("userId");
    let accoun = wx.getStorageSync("accoun");
    let parkColumns = wx.getStorageSync("parkColumns");
    this.setData({
      managerId: managerId ? managerId : "",
      "userInfo.nickName": accoun ? accoun : "",
      parkId: parkId ? parkId : "",
      parkColumns: parkColumns ? parkColumns : [],
    });
    this.getParkInfoList();
    if (parkId) {
      this.getTicketbalance();
    }
  },
  // 选择停车场
  bindChangeParkName(e) {
    this.setData({
      parkId: e.detail.parkId,
    });
    this.getTicketbalance();
    this.getParkInfoList();
  },

  /**
   * 退出登录
   */
  logout() {
    wx.showModal({
      title: "操作确认",
      content: "是否确认退出登录？",
      confirmText: "确认退出",
      success(res) {
        if (res.confirm) {
          wx.reLaunch({
            url: "/pages/login/login",
          });
          // 清除本地缓存
          wx.clearStorageSync();
          wx.setStorageSync("isLogout", true);
        }
      },
    });
  },

  /**
   * 获取车场管理员列表
   */
  getParkManagerList() {
    let that = this;
    let parmas = {
      currentPage: this.data.currentPage,
      size: this.data.pageSize,
    };
    request.http("/park/park-manager/page", "GET", parmas).then((res) => {
      let data = res.data;
      if (data.code == 200) {
        that.setData({
          parkManagerList: data.data.records,
        });
      }
    });
  },

  // 跳转页面
  async toPage(e) {
    const isParkId = getApp().isRegisterPark();
    if (isParkId) {
      const { url } = e.currentTarget.dataset;
      wx.navigateTo({
        url,
      });
    }
  },

  //   查询停车场列表
  async getParkInfoList() {
    const { managerId, pageNum, pageSize } = this.data;
    const { data: res } = await getParkInfoListApi({
      // parkManagerId: managerId,
      pageNum,
      pageSize,
    });
    
    if (res.data.records.length > 0) {
      const lastIndex = res.data.records.length - 1;
      const totalIncome = res.data.records[lastIndex];
      this.setData({
        "userInfo.realName": res.data.records[0].juridicalPersonName,
        "userInfo.phone": res.data.records[0].telephone,
        "userInfo.nickName": wx.getStorageSync("accoun"),
        parkingList: res.data.records,
        parkName: res.data.records[0].parkName,
        totalIncome,
      });
    }
  },
  //   修改车场信息
  seeOrEditParkInfo(e) {
    const { item, type } = e.currentTarget.dataset;
    wx.setStorageSync("parkInfoDetail", item);
    wx.navigateTo({
      url: "/pages/addParking/addParking?type=" + type,
    });
  },

  // 查询通行券余额
  async getTicketbalance() {
    try {
      const { data: res } = await getTicketbalanceApi({
        parkId: this.data.parkId,
      });
      this.setData({
        ticketBlance: res.data,
        autoSettledStatus: res.data.autoSettled,
      });
      // console.log("结算状态0未开启，1已开启", this.data.autoSettledStatus);
    } catch (error) {}
  },

  // 设置自动结算
  async setAutoSettled() {
    if (!this.data.parkId) {
      getApp().showToast("请先添加停车场");
      return;
    }
    const { autoSettledStatus } = this.data;
    let text = autoSettledStatus === 1 ? "关闭" : "开启";
    wx.showModal({
      title: "提示",
      content: `是否${text}自动结算功能`,
      confirmText: `确认${text}`,
      complete: (res) => {
        if (res.confirm) {
          this.autoSettled();
        }
      },
    });
  },
  // 自动结算api
  async autoSettled() {
    const { autoSettledStatus } = this.data;
    const { data: res } = await setAutoSettledApi({
      parkId: this.data.parkId,
      status: autoSettledStatus === 1 ? 0 : 1,
    });
    this.getTicketbalance();
  },
});
